package com.ruicar.afs.cloud.account.common.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.account.common.entity.VoucherBusinessConfiguration;
import com.ruicar.afs.cloud.account.common.entity.VoucherInfo;
import com.ruicar.afs.cloud.account.common.mapper.VoucherBusinessConfigurationMapper;
import com.ruicar.afs.cloud.account.common.service.VoucherBusinessConfigurationService;
import com.ruicar.afs.cloud.account.common.service.VoucherInfoService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.BusinessType;
import com.ruicar.afs.cloud.common.modules.contract.enums.VoucherStatus;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/7/12 15:37
 */
@Service
@AllArgsConstructor
public class VoucherBusinessConfigurationServiceImpl extends ServiceImpl<VoucherBusinessConfigurationMapper, VoucherBusinessConfiguration> implements VoucherBusinessConfigurationService {
    private final VoucherInfoService voucherInfoService;

    @Override
    public Boolean addVoucherBusiness(VoucherBusinessConfiguration voucherBusinessConfiguration) {
        check(voucherBusinessConfiguration);
        return this.save(voucherBusinessConfiguration);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delVoucherBusiness(String id) {
        VoucherBusinessConfiguration voucherBusinessConfiguration = this.getById(id);
        Assert.isTrue(voucherBusinessConfiguration != null, "删除失败，凭证事件配置不存在!");
        long count = voucherInfoService.count(Wrappers.<VoucherInfo>lambdaQuery()
                .eq(voucherBusinessConfiguration.getBusinessType().equals(BusinessType.MAIN), VoucherInfo::getMainBusinessCode, voucherBusinessConfiguration.getBusinessCode())
                .eq(voucherBusinessConfiguration.getBusinessType().equals(BusinessType.CHILD), VoucherInfo::getChildBusinessCode, voucherBusinessConfiguration.getBusinessCode()));
        Assert.isTrue(count <= 0, "业务事件代码[" + voucherBusinessConfiguration.getBusinessCode() + "]已被使用不可删除！");
        if (BusinessType.MAIN.equals(voucherBusinessConfiguration.getBusinessType())) {
            return this.remove(Wrappers.<VoucherBusinessConfiguration>query().lambda()
                    .eq(VoucherBusinessConfiguration::getMainBusinessCode, voucherBusinessConfiguration.getBusinessCode())
                    .or().eq(VoucherBusinessConfiguration::getId, voucherBusinessConfiguration.getId()));
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyVoucherBusiness(VoucherBusinessConfiguration voucherBusinessConfiguration) {
        check(voucherBusinessConfiguration);
        VoucherBusinessConfiguration vonfiguration = this.getById(voucherBusinessConfiguration.getId());
        long count = voucherInfoService.count(Wrappers.<VoucherInfo>lambdaQuery()
                .eq(voucherBusinessConfiguration.getBusinessType().equals(BusinessType.MAIN), VoucherInfo::getMainBusinessCode, vonfiguration.getBusinessCode())
                .eq(voucherBusinessConfiguration.getBusinessType().equals(BusinessType.CHILD), VoucherInfo::getChildBusinessCode, vonfiguration.getBusinessCode())
                .eq(VoucherInfo::getStatus, VoucherStatus.ACTIVE ));
        Assert.isTrue(count <= 0, "业务事件代码[" + vonfiguration.getBusinessCode() + "]已被使用不可修改！");
        if(voucherBusinessConfiguration.getBusinessType().equals(BusinessType.MAIN)){
            voucherInfoService.update(Wrappers.<VoucherInfo>lambdaUpdate()
                    .eq(VoucherInfo::getMainBusinessCode,vonfiguration.getBusinessCode())
                    .set(VoucherInfo::getMainBusiness, voucherBusinessConfiguration.getBusiness()));
        }else{
            voucherInfoService.update(Wrappers.<VoucherInfo>lambdaUpdate()
                    .eq(VoucherInfo::getChildBusinessCode,vonfiguration.getBusinessCode())
                    .set(VoucherInfo::getChildBusiness, voucherBusinessConfiguration.getBusiness()));
        }
        return this.updateById(voucherBusinessConfiguration);
    }

    @Override
    public IResponse query(QueryCondition<VoucherBusinessConfiguration> queryCondition) {
        IPage iPage = this.page(new Page<>(queryCondition.getPageNumber(), queryCondition.getPageSize()),
                Wrappers.<VoucherBusinessConfiguration>lambdaQuery()
                        .like(StringUtils.isNotEmpty(queryCondition.getCondition().getBusiness()), VoucherBusinessConfiguration::getBusiness, queryCondition.getCondition().getBusiness())
                        .like(StringUtils.isNotEmpty(queryCondition.getCondition().getBusinessCode()), VoucherBusinessConfiguration::getBusinessCode, queryCondition.getCondition().getBusinessCode())
                        .like(StringUtils.isNotEmpty(queryCondition.getCondition().getBusinessDescribe()), VoucherBusinessConfiguration::getBusinessDescribe, queryCondition.getCondition().getBusinessDescribe())
                        .eq(VoucherBusinessConfiguration::getBusinessType, BusinessType.MAIN).orderByDesc(VoucherBusinessConfiguration::getCreateTime));
        return IResponse.success(iPage);

    }

    @Override
    public IResponse queryBusinessList(VoucherBusinessConfiguration queryCondition) {
        List<VoucherBusinessConfiguration> list = this.list(Wrappers.<VoucherBusinessConfiguration>lambdaQuery().eq(VoucherBusinessConfiguration::getBusinessType, queryCondition.getBusinessType()).orderByDesc(VoucherBusinessConfiguration::getCreateTime));
        return IResponse.success(list);
    }

    @Override
    public IResponse queryListChild(VoucherBusinessConfiguration queryCondition) {
        List<VoucherBusinessConfiguration> list = this.list(Wrappers.<VoucherBusinessConfiguration>lambdaQuery()
                .eq(VoucherBusinessConfiguration::getMainBusinessCode, queryCondition.getMainBusinessCode())
                .eq(VoucherBusinessConfiguration::getBusinessType, BusinessType.CHILD).orderByDesc(VoucherBusinessConfiguration::getCreateTime));
        return IResponse.success(list);

    }

    private void check(VoucherBusinessConfiguration voucherBusinessConfiguration) {
        long count = this.baseMapper.selectCount(Wrappers.<VoucherBusinessConfiguration>lambdaQuery()
                .eq(VoucherBusinessConfiguration::getBusinessCode, voucherBusinessConfiguration.getBusinessCode()).ne(voucherBusinessConfiguration.getId() != null, VoucherBusinessConfiguration::getId, voucherBusinessConfiguration.getId()));
        Assert.isTrue(count < 1, "业务事件代码 [" + voucherBusinessConfiguration.getBusinessCode() + "]重复");
    }
}
